<template>
  <div class="container">
    <el-dialog title="添加" :visible.sync="dialogShow" width="1000px" top="4vh">
      <!-- 第一行——操作按钮 -->
      <div class="filter-container">
        关键字或手机号：
        <el-input v-model="listQuery.queryValue" class="mr20" placeholder="请输入" style="width: 300px" clearable />
        <el-button class="el-icon-search" type="primary" style="width: 80px" @click="handleFilter">查询</el-button>
      </div>
      <!-- / 第一行——操作按钮 -->

      <!-- table表格 -->
      <el-table ref="multipleTable" :data="list" :stripe="true" size="medium" style="width: 100%" max-height="500" @selection-change="handleSelectionChange">
        <!--      <el-table-column type="selection" width="160px" > </el-table-column>  @selection-change="handleSelectionChange"-->

        <!--        <el-table-column-->
        <!--          type="selection"-->
        <!--          width="55">-->
        <!--        </el-table-column>-->
        <!-- 标题 -->
        <el-table-column align="center" prop="name" label="姓名" width="300px" />
        <el-table-column align="center" prop="phone" label="手机号码" />

        <!-- 操作-->
        <el-table-column label="操作" width="200px" fixed="right" align="center">
          <template slot-scope="{ row, $index }">
            <el-button v-if="1!=row.addStatus" type="text" @click="addItem(row, $index)">添加</el-button>
            <el-button v-else type="text" @click="delItem(row, $index)">删除</el-button>
            <!--            <el-button   type="text" @click="delItem(row, $index)">删除</el-button>-->

          </template>
        </el-table-column>
        <!-- / 操作 -->

      </el-table>
      <!-- / table表格 -->

      <!-- 分页 -->
      <pagination
        v-show="total > 0"
        :total="total"
        next-text="下一页"
        layout="prev, pager, next, jumper"
        :page.sync="listQuery.pageNo"
        :limit.sync="listQuery.pageSize"
        @current-change="currentChange"
        @pagination="getList"
      />

    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // 分页

import { sys_role_addSysUserRole, sys_role_deleteUserRole } from '@/api/sys_role'
import { sys_user_queryUserRole } from '@/api/sys_user'

import { list } from '@/api/colleague'

export default {
  components: { Pagination },
  props: {},
  data() {
    return {
      roleId: '',
      phone: '', //  搜索
      // 时间选择器
      list: [], // 内容列表
      total: 0, // 总页数
      listLoading: true,

      // 查询参数
      listQuery: {
        queryValue: null,
        pageNo: 1,
        pageSize: 10
      },
      dialogShow: false
    }
  },
  watch: {
    dialogShow(e) {
      if (e) {
        this.listQuery = {
          queryValue: null,
          pageNo: 1,
          pageSize: 10
        }
        this.getList()
      }
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  // 销毁
  destroyed() {},
  // 方法
  methods: {
    cancelForm() {
      this.loading = false
      this.dialog = false
      // clearTimeout(this.timer);
    },
    // 分页数据
    getList() {
      console.log('-----获得分页数据-------')
      this.listLoading = true

      this.listQuery.phone = this.phone

      list(this.listQuery).then(response => {
        // this.list = response.result.records
        // for (let i = 0; i < this.list.length; i++) {
        //   this.list[i].addStatus = 0
        // }

        const list = response.result.records

        for (let i = 0; i < list.length; i++) {
          list[i].addStatus = 0
          // let  element=list[i];
          // console.log(element);
        }
        this.list = list
        this.total = response.result.total
        this.listLoading = false

        this.initAadStatus(this.list)
      })
    },
    // 分页数据
    initAadStatus(list) {
      for (let i = 0; i < list.length; i++) {
        sys_user_queryUserRole({ 'userid': list[i].id }).then(response2 => {
          // console.log("response2.result"+response2.result,this.roleId,response2.result.indexOf(this.roleId));
          if (response2.result.indexOf(this.roleId) >= 0) {
            list[i].addStatus = 1
            //  console.log(this.list[i].name,"未添加");
            // console.log(list[i].name,list[i].addStatus);
          }

          // else {
          //   console.log(this.list[i].name,"已添加");
          //    list[i].addStatus =1
          // }
          // this.list=list;
          // let  element=list[i];
        })
      }
    },

    // 搜索
    handleFilter() {
      this.listQuery.page = 1

      console.log('search:' + this.phone)
      // if(''!=this.title&&null!=this.title){
      //   this.listQuery.title = "*"+this.title+"*";
      // }else {
      //   this.listQuery.title ="";
      // }

      this.getList(this.listQuery)
    },
    currentChange(e) {
      console.log('currentChange' + e)
    },

    handleSelectionChange(val) {
      console.log('handleSelectionChange' + val)
      this.multipleSelection = val
    },

    // 删除
    delItem(row, index) {
      console.log('--------------', row.id)
      const p = {
        params: {	// 请求参数拼接在url上
          roleId: this.roleId,
          userId: row.id
        }
      }

      this.$confirm('确定要移除吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        sys_role_deleteUserRole(p).then(response => {
          row.addStatus = 0
          this.$message({
            type: 'success',
            message: '删除成功!'
          })

          // this.$emit('refresh')
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 保存
    addItem(row) {
      // console.log("submit2")
      const role = { 'roleId': '', 'userIdList': [] }// {"roleId":"1336118591098998785","userIdList":["1287687492661800962","1179675874431131649","e9ca23d68d884d4ebb19d07889727dae"]}
      role.roleId = this.roleId
      role.userIdList.push(row.id)

      sys_role_addSysUserRole(role).then(response => {
        if (response.code == '200') {
          this.$message({
            type: 'success',
            message: '添加成功!'
          })
          row.addStatus = 1// 已经添加
        } else {
          this.$message({
            type: 'error',
            message: '添加失败!'
          })
        }

        // this.drawerShow = false
        this.$emit('refresh')
      })
    }, // end of function
    show(roleId) {
      this.roleId = roleId

      console.log('打开窗口:', roleId)
      this.getList()
      this.dialogShow = true
    }
  }
}
</script>
<style lang="scss" scoped>
  .pagination-container{
      padding: 0;
      margin: 20px 0;
  }
</style>
